package com.eascs.ticket.user.interceptor;

import javax.inject.Inject;

import org.springframework.stereotype.Component;

import com.eascs.ticket.common.entity.StatusConstant;
import com.eascs.ticket.user.service.UserService;
import com.eascs.web.sso.MenuItem;
import com.eascs.web.sso.SsoUser;
import com.eascs.web.sso.SsoUserFilter;
import com.eascs.web.sso.User;

@Component
public class TicketSsoUserFilter implements SsoUserFilter {

	@Inject
	private UserService userService;

	@Override
	public void filter(SsoUser ssoUser) {
		User user = ssoUser.getUser();
		if (null == user)
			return;
		if ("muwang.zheng".equals(user.getAccount()) || "jun.lu".equals(user.getAccount()) || "hongquan.tang".equals(user.getAccount())) {
			user.setSuperAdministrator(true);
		}
		user.getMenuItemTree().clear();
		user.getPermissionCodeMap().clear();
		user.getPermissionUrlMap().clear();
		com.eascs.ticket.user.entity.User userEntity = new com.eascs.ticket.user.entity.User();
		userEntity.setCode(user.getCode());
		userEntity.setLoginName(user.getAccount());
		userEntity.setName(user.getCname());
		userEntity.setEmail(user.getEmail());
		userEntity.setPhone(user.getPhone());
		userEntity.setStatus(StatusConstant.STATUS_OK);
		userService.saveUser(userEntity);

		buildMenu(user);

		buidSystemMenu(user);
	}

	private void buildMenu(User user) {
		MenuItem ticketMenuItem = new MenuItem();
		user.getMenuItemTree().add(ticketMenuItem);
		ticketMenuItem.setName("工单相关");
		ticketMenuItem.setUrl("#");
		ticketMenuItem.setCode("ticketMenuItem");
		ticketMenuItem.getSubMenuItems().add(new MenuItem("增加工单", "/ticket/add"));
		ticketMenuItem.getSubMenuItems().add(new MenuItem("查看所有未关闭的工单表（按到期时间排序）", "/ticket/view/search?orderBy=dueDate&statusNotIns=CLOSE&statusNotIns=CANCEL&pageSize=100"));
		ticketMenuItem.getSubMenuItems().add(new MenuItem("查看所有未关闭的工单表（按版本时间排序）", "/ticket/view/search?orderBy=version&statusNotIns=CLOSE&statusNotIns=CANCEL&pageSize=100"));
		ticketMenuItem.getSubMenuItems().add(new MenuItem("查看自动创建工单列表", "/schedule/view/viewScheduleTicketList"));
		ticketMenuItem.getSubMenuItems().add(new MenuItem("查看本人自动转移设置", "/configuration/userAutoAssign/my"));

		MenuItem teamMenuItem = new MenuItem();
		user.getMenuItemTree().add(teamMenuItem);
		teamMenuItem.setName("团队信息");
		teamMenuItem.setUrl("#");
		teamMenuItem.setCode("teamMenuItem");
		teamMenuItem.getSubMenuItems().add(new MenuItem("查看团队工单情况", "/ticket/view/operators"));
		teamMenuItem.getSubMenuItems().add(new MenuItem("查看组织结构", "/user/structure/view"));

		MenuItem versionMenuItem = new MenuItem();
		user.getMenuItemTree().add(versionMenuItem);
		versionMenuItem.setName("版本工单");
		versionMenuItem.setUrl("#");
		versionMenuItem.setCode("versionMenuItem");
		versionMenuItem.getSubMenuItems().add(new MenuItem("查看版本发布注意事项", "/ticket/releaseNote/findTicketView"));
		versionMenuItem.getSubMenuItems().add(new MenuItem("查看所有未关闭的版本", "/ticket/view/search?ticketTypeCodes=version&statusNotIns=CLOSE"));
		versionMenuItem.getSubMenuItems().add(new MenuItem("看所有未关闭的版本工单表", "/ticket/view/versions"));
		versionMenuItem.getSubMenuItems().add(new MenuItem("查看所有未关闭非版本类型的工单表", "/ticket/view/search?orderBy=version&ticketTypeCodeNotIns=version&statusNotIns=CLOSE&pageSize=100"));
		versionMenuItem.getSubMenuItems().add(new MenuItem("查看所有未关闭带版本类型的工单表", "/ticket/view/search?orderBy=version&statusNotIns=CLOSE&pageSize=100"));

		MenuItem statisticMenuItem = new MenuItem();
		user.getMenuItemTree().add(statisticMenuItem);
		statisticMenuItem.setName("工作统计");
		statisticMenuItem.setUrl("#");
		statisticMenuItem.setCode("statisticMenuItem");
		statisticMenuItem.getSubMenuItems().add(new MenuItem("查看本周工作工单情况", "/stat/view/work/this-week"));
		statisticMenuItem.getSubMenuItems().add(new MenuItem("查看上周工作工单情况", "/stat/view/work/last-week"));
		statisticMenuItem.getSubMenuItems().add(new MenuItem("查看工时排行（本年）", "/ticket/statistics/view/overtime"));
		statisticMenuItem.getSubMenuItems().add(new MenuItem("查看个人工单排行（本年）", "/ticket/statistics/view/tickets?groupBy=creator&type=thisYear"));
		statisticMenuItem.getSubMenuItems().add(new MenuItem("查看个人工单排行（本周）", "/ticket/statistics/view/tickets?groupBy=creator&type=thisWeek"));
		statisticMenuItem.getSubMenuItems().add(new MenuItem("查看个人工单排行（上周）", "/ticket/statistics/view/tickets?groupBy=creator&type=lastWeek"));
		statisticMenuItem.getSubMenuItems().add(new MenuItem("查看个人工单排行（本月）", "/ticket/statistics/view/tickets?groupBy=creator&type=thisMonth"));
		statisticMenuItem.getSubMenuItems().add(new MenuItem("查看个人工单排行（上月）", "/ticket/statistics/view/tickets?groupBy=creator&type=lastMonth"));
		statisticMenuItem.getSubMenuItems().add(new MenuItem("工作日配置信息", "/workday/list"));

	}

	private void buidSystemMenu(User user) {
		if (user.isSuperAdministrator()) {
			MenuItem settingMenuItem = new MenuItem();
			user.getMenuItemTree().add(settingMenuItem);
			settingMenuItem.setName("系统设置");
			settingMenuItem.setUrl("#");
			settingMenuItem.setCode("settingMenuItem");
			settingMenuItem.getSubMenuItems().add(new MenuItem("查看状态清单", "/configuration/status/list"));
			settingMenuItem.getSubMenuItems().add(new MenuItem("查看工单类型清单", "/configuration/ticket-type/list"));
			settingMenuItem.getSubMenuItems().add(new MenuItem("查看数据字典类型", "/configuration/dictionary/viewDictionaryData"));
			settingMenuItem.getSubMenuItems().add(new MenuItem("查看数据类型", "/configuration/dictionary/viewDictionaryType"));
			settingMenuItem.getSubMenuItems().add(new MenuItem("查看序号清单", "/configuration/dictionary/viewDictionarySeq"));
			settingMenuItem.getSubMenuItems().add(new MenuItem("项目状态备注模板", "/configuration/projectCommentTemplate/list"));
			settingMenuItem.getSubMenuItems().add(new MenuItem("新增项目", "/project/create"));
		}
	}

}
